package com.haoshuang.zhouzhoubang.ui.page.order.upload

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityUploadInServiceBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderBean5
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.haoshuang.zhouzhoubang.ui.dialog.ShowUploadExampleDialog
import com.haoshuang.zhouzhoubang.widget.UploadFileBean
import com.luck.picture.lib.entity.LocalMedia
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.NetworkUtil.url
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *  上传服务中图片和视频
 */
class UploadInServiceActivity :
    BaseVMActivity<UploadInServiceViewModel, ActivityUploadInServiceBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<UploadInServiceActivity>()
        }
    }

    private var orderBean5: OrderBean5? = null
    private var orderId = ""

    override fun initIntent() {
        super.initIntent()
        orderId = intent.getString("orderId")
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply {
            titleBar.title = "上传服务中图片"
            titleBar.leftImageView().click { finish() }

            titleBar.rightImageView().clickWithTrigger {
                XPopup.Builder(mActivity)
                    .asCustom(
                        ConfirmDialog(
                            context = mActivity,
                            title = "上传视频奖励",
                            content = "需上传服务中视频有${orderBean5?.upload_video_award ?: "0"}元上传奖励",
                            cancelStr = "",
                            confirmStr = "我知道了",
                            isHideCancel = true,
                            onCancel = {},
                            onConfirm = {})
                    )
                    .show()
            }

            tvUploadExample.clickWithTrigger {
                XPopup.Builder(mActivity)
                    .asCustom(
                        ShowUploadExampleDialog(
                            context = mActivity,
                            order_id = orderId.toString(),
                            type = 5
                        )
                    )
                    .show()
            }

            tvSubmit.clickWithTrigger {
                val list = customUploadView.getMediaList()
                if (list.isEmpty()) {
                    toast("请上传图片或视频")
                    return@clickWithTrigger
                }
                val imageList = list.filter { !it.isVideo }
                val video = list.firstOrNull() { it.isVideo }

                vm.getData012(
                    "id" to orderId,
                    "service_img" to imageList.joinToString(",") { it.url.toString() },
                    "service_video" to video
                )
            }
        }
    }

    override fun initData() {
        vm.getData("id" to orderId)
        vm.getData5("id" to orderId)
        vm.getData29("id" to orderId)
    }

    override fun startObserve() {
        vm.getData012.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                setResult(200)
                finish()
            }
        }
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()

            }
        }

        vm.getData29.observeState(this) {
            onSuccess {
                if (it != null) {
                    val list = arrayListOf<UploadFileBean>()
                    if (!it?.service_img.isNullOrBlank()) {
                        var arr = it.service_img.split(",").filter { it.isNotEmpty() }
                        for (b in arr) {
                            list.add(UploadFileBean(null, b, 0, false, 2))
                        }
                    }
                    if (!it.service_video.isNullOrBlank()) {
                        list.add(UploadFileBean(null, it.service_video, 0, true, 2))
                    }
                    binding.customUploadView.setMediaList(list)
                }

            }
        }

        vm.getData5.observeState(this) {
            onSuccess {
                orderBean5 = it
            }
        }
    }

}